/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package com.jzwl.site.wap.wxUser.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("v2wxUserDao")
public class WxUserDao {

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库

	/**
	 * 存储用户信息
	 * @Date 2019年5月13日
	 * @author gyp
	 * @param map
	 */
	public boolean addWxUser(Map<String, Object> map) {

		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		//0:否，1:是
		Date date = new Date();
		map.put("createTime", date);
		map.put("isDelete", 0);
		map.put("status", 0);
		map.put("isBlack", 0);

		String sql = "insert into wx_user "
				+ " (openId,wxname,name,phone,sex,isBlack,status,isDelete,createTime,headImgUrl) " + " values "
				+ " (:openId,:wxname,:name,:phone,:sex,:isBlack,:status,:isDelete,:createTime,:headImgUrl)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {
		return "" + " openId as openId," + " wxname as wxname," + " name as name," + " phone as phone,"
				+ " labelId as labelId," + " sex as sex," + " address as address," + " country as country,"
				+ " province as province," + " city as city," + " longitude as longitude," + " latitude as latitude,"
				+ " isBlack as isBlack," + " status as status," + " isDelete as isDelete,"
				+ " createTime as createTime," + " remark as remark," + " headImgUrl as headImgUrl";
	}

	public PageObject queryWxUserList(Map<String, Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性

		String sql = "select " + getColumns() + " from v2_wx_user t where 1=1 ";

		if (null != map.get("wxname") && StringUtils.isNotEmpty(map.get("wxname").toString())) {
			sql = sql + " and t.wxname  = " + map.get("wxname") + "";
		}
		if (null != map.get("name") && StringUtils.isNotEmpty(map.get("name").toString())) {
			sql = sql + " and t.name  like '%" + map.get("name") + "%' ";
		}
		if (null != map.get("phone") && StringUtils.isNotEmpty(map.get("phone").toString())) {
			sql = sql + " and t.phone  like '%" + map.get("phone") + "%' ";
		}
		if (null != map.get("labelId") && StringUtils.isNotEmpty(map.get("labelId").toString())) {
			sql = sql + " and t.labelId  = " + map.get("labelId") + "";
		}
		if (null != map.get("sex") && StringUtils.isNotEmpty(map.get("sex").toString())) {
			sql = sql + " and t.sex  = " + map.get("sex") + "";
		}
		if (null != map.get("address") && StringUtils.isNotEmpty(map.get("address").toString())) {
			sql = sql + " and t.address  = " + map.get("address") + "";
		}
		if (null != map.get("country") && StringUtils.isNotEmpty(map.get("country").toString())) {
			sql = sql + " and t.country  = " + map.get("country") + "";
		}
		if (null != map.get("province") && StringUtils.isNotEmpty(map.get("province").toString())) {
			sql = sql + " and t.province  = " + map.get("province") + "";
		}
		if (null != map.get("city") && StringUtils.isNotEmpty(map.get("city").toString())) {
			sql = sql + " and t.city  = " + map.get("city") + "";
		}
		if (null != map.get("longitude") && StringUtils.isNotEmpty(map.get("longitude").toString())) {
			sql = sql + " and t.longitude  = " + map.get("longitude") + "";
		}
		if (null != map.get("latitude") && StringUtils.isNotEmpty(map.get("latitude").toString())) {
			sql = sql + " and t.latitude  = " + map.get("latitude") + "";
		}
		if (null != map.get("isBlack") && StringUtils.isNotEmpty(map.get("isBlack").toString())) {
			sql = sql + " and t.isBlack  = " + map.get("isBlack") + "";
		}
		if (null != map.get("status") && StringUtils.isNotEmpty(map.get("status").toString())) {
			sql = sql + " and t.status  = " + map.get("status") + "";
		}
		if (null != map.get("isDelete") && StringUtils.isNotEmpty(map.get("isDelete").toString())) {
			sql = sql + " and t.isDelete  = " + map.get("isDelete") + "";
		}
		if (null != map.get("createTime") && StringUtils.isNotEmpty(map.get("createTime").toString())) {
			sql = sql + " and t.createTime >= " + map.get("createTimeBegin") + "";
			sql = sql + " and t.createTime <= " + map.get("createTimeEnd") + "";
		}
		if (null != map.get("remark") && StringUtils.isNotEmpty(map.get("remark").toString())) {
			sql = sql + " and t.remark  = " + map.get("remark") + "";
		}
		if (null != map.get("headImgUrl") && StringUtils.isNotEmpty(map.get("headImgUrl").toString())) {
			sql = sql + " and t.headImgUrl  = " + map.get("headImgUrl") + "";
		}

		sql = sql + " order by openId ";

		PageObject po = baseDAO.queryForMPageList(sql, new Object[] {}, map);

		return po;
	}

	public boolean updateWxUser(Map<String, Object> map) {

		String sql = "update v2_wx_user set "
				+ " openId=:openId,wxname=:wxname,name=:name,phone=:phone,labelId=:labelId,sex=:sex,address=:address,country=:country,province=:province,city=:city,longitude=:longitude,latitude=:latitude,isBlack=:isBlack,status=:status,isDelete=:isDelete,createTime=:createTime,remark=:remark,headImgUrl=:headImgUrl "
				+ " where openId=:openId";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteWxUser(Map<String, Object> map) {

		String sql = "delete from v2_wx_user where openId in (" + map.get("openId") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	/**
	 * 根据openId获取用户信息 
	 * @Date 2019年5月13日
	 * @author gyp
	 * @param map
	 */
	public Map<String, Object> queryUserInfoByOpenId(String openId) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		String sql = "select " + getColumns() + " from wx_user where openId = '" + openId + "'";
		resMap = baseDAO.queryForMap(sql);
		return resMap;
	}

}
